package com.games.ecoship.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import com.games.ecoship.EcoShipConstants;

public class Cell extends JPanel implements EcoShipConstants {
	
	private static final long  serialVersionUID = 2815270714557507265L;
	private static final int   OFFSET       = 12;
	private static final int   BORDER_WIDTH = 1;
	private static final int   CELL_SIZE    = 30;
	private static final Color CELL_COLOR   = Color.cyan;
	
	private static EcoShipClient ref = null;
	
	// Position of this cell
	private int col;
	private int row;
	
	// The board that this cell belongs to
	private int playerNum;
	
	// Token occupying this cell
	private char token = EMPTY_SPOT;
	
	public Cell(int row, int col) {
		this.setPreferredSize(new Dimension(CELL_SIZE,CELL_SIZE));
		this.setRow(row);
		this.setCol(col);
		setBorder(new LineBorder(Color.black, BORDER_WIDTH));
		addMouseListener(new ClickListener());
	}
	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		g.setColor(CELL_COLOR);
		g.fillRect(0, 0, getWidth(), getHeight());
		// Draw an empty circle (indicating a blank spot)
		if (token == EMPTY_SPOT) {
			g.setColor(Color.black);
			g.drawOval(OFFSET, OFFSET, getWidth()-OFFSET*2, getHeight()-OFFSET*2);
		}
		// Draw a 'hit' spot (red fill color, and black outline)
		else if (token == HIT_SPOT) {
			g.setColor(Color.red);
			g.fillOval(OFFSET, OFFSET, getWidth()-OFFSET*2, getHeight()-OFFSET*2);
			g.setColor(Color.black);
			g.drawOval(OFFSET, OFFSET, getWidth()-OFFSET*2, getHeight()-OFFSET*2);
		}
		// Draw a 'miss' spot (white fill color, and black outline)
		else if (token == MISS_SPOT) {
			g.setColor(Color.white);
			g.fillOval(OFFSET, OFFSET, getWidth()-OFFSET*2, getHeight()-OFFSET*2);
			g.setColor(Color.black);
			g.drawOval(OFFSET, OFFSET, getWidth()-OFFSET*2, getHeight()-OFFSET*2);
		}
		// Draw a 'miss' spot (white fill color, and black outline)
		else {
			//... TODO: Consider how to draw a ship once all the spots have been hit, or if it is the player's ships
			g.drawString(String.valueOf(token), getWidth()/2, getHeight()/2);
		}
	}
	
	public char getToken() {
		return token;
	}
	
	public void setToken(char token) {
		this.token = token;
		repaint();
	}
	
	public static void setReference(EcoShipClient reference) {
		ref = reference;
	}

	public EcoShipClient getReference() {
		return ref;
	}
	
	public void setRow(int row) {
		this.row = row;
	}

	public int getRow() {
		return row;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public int getCol() {
		return col;
	}
	
	public void setPlayerNum(int playerNum) {
		this.playerNum = playerNum;
	}

	public int getPlayerNum() {
		return playerNum;
	}
}
